//
//  FSWToast.m
//  TuanLife
//
//  Created by 伏圣文 on 2017/12/23.
//  Copyright © 2017年 HuiNaMac. All rights reserved.
//

#import "FSWToast.h"
#import <QuartzCore/QuartzCore.h>

#ifndef SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#endif

@implementation FSWToast

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(flipViewAccordingToStatusBarOrientation:) name:UIDeviceOrientationDidChangeNotification object:nil];
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)show {
    __block typeof(self) weakSelf = self;
    
    [UIView animateWithDuration:0.3f
                     animations: ^{
                         weakSelf.alpha = 1.0f;
                         weakSelf.transform = CGAffineTransformScale(weakSelf.transform, 1.0f, 1.0f);
                     }
     
                     completion: ^(BOOL finished) {
                         [weakSelf performSelector:@selector(hide)
                                        withObject:nil
                                        afterDelay:1];
                     }];
}

- (void)hide {
    __block typeof(self) weakSelf = self;
    
    [UIView animateWithDuration:0.4f
                     animations: ^{
                         weakSelf.alpha = 0.0f;
                         weakSelf.transform = CGAffineTransformScale(weakSelf.transform, 0.1f, 0.1f);
                     }
     
                     completion: ^(BOOL finished) {
                         [weakSelf removeFromSuperview];
                     }];
}

+ (FSWToast *)createWithText:(NSString *)text {
    UILabel *textLabel = [[UILabel alloc] init];
    textLabel.backgroundColor = [UIColor clearColor];
    textLabel.textAlignment = NSTextAlignmentCenter;
    textLabel.font = [UIFont systemFontOfSize:14];
    textLabel.textColor = [UIColor whiteColor];
    textLabel.numberOfLines = MAXFLOAT;
    textLabel.lineBreakMode = NSLineBreakByCharWrapping;
    textLabel.text = text;
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGSize size = CGSizeZero;
    size = [text boundingRectWithSize:CGSizeMake(width - 60, CGFLOAT_MAX)
                              options:NSStringDrawingUsesLineFragmentOrigin
                           attributes:@{NSFontAttributeName: textLabel.font}
                              context:nil].size;
    
    if (size.width < 50) {
        size.width = 50;
    }
    
    if (size.height < 20) {
        size.height = 20;
    }else{
        textLabel.textAlignment = NSTextAlignmentLeft;
    }
    FSWToast *toast = [[FSWToast alloc] initWithFrame:CGRectMake(0, 0, size.width + 30, size.height + 30)];
    toast.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8f];
    CALayer *layer = toast.layer;
    layer.masksToBounds = YES;
    layer.cornerRadius = 5.0f;
    
    textLabel.frame = CGRectMake(5, 5, toast.bounds.size.width - 10, toast.bounds.size.height - 10);
    [toast addSubview:textLabel];
    
    toast.alpha = 0.0f;
    return toast;
}

- (void)flipViewAccordingToStatusBarOrientation:(id)sender {
    float angle = 0.0f;
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    float x = 0.0f, y = 0.0f;
    switch (orientation) {
        case UIInterfaceOrientationPortraitUpsideDown:
        {
            angle = M_PI;
            break;
        }
            
        case UIInterfaceOrientationLandscapeLeft:
        {
            angle = -M_PI / 2.0f;
            x = screenSize.width / 2.0f;
            y = screenSize.height / 2.0f;
            break;
        }
            
        case UIInterfaceOrientationLandscapeRight:
        {
            angle = M_PI / 2.0f;
            x = screenSize.width / 2.0f;
            y = screenSize.height / 2.0f;
            break;
        }
            
        default:
        {
            angle = 0.0;
            x = screenSize.width / 2.0f;
            y = screenSize.height / 2.0f;
            break;
        }
    }
    
    float duration = 0.3f;
    if ([sender isKindOfClass:[NSNumber class]]) {
        BOOL type = [(NSNumber *)sender boolValue];
        if (!type) {
            duration = 0.0f;
        }
    }
    [UIView animateWithDuration:duration animations: ^{
        self.center = CGPointMake(x, y);
        self.transform = CGAffineTransformMakeRotation(angle);
    } completion: ^(BOOL finished) {
    }];
}

+ (void)showWithText:(NSString *)text {
    FSWToast *toast = [FSWToast createWithText:text];
    UIWindow *mainWindow;// = [[[UIApplication sharedApplication] windows] lastObject];
    if (!mainWindow) {
        mainWindow = [[UIApplication sharedApplication] keyWindow];
    }
    [mainWindow addSubview:toast];
    [toast flipViewAccordingToStatusBarOrientation:@(NO)];
    [toast show];
}

@end
